package cn.bisoft.java.test;

import javassist.ClassPool;
import javassist.CtClass;

public class InjectUtil
{

	public byte[] writeClassFile(CtClass cc, String directoryName, boolean stopPruning)
	{
		if (stopPruning)
		{
			cc.stopPruning(stopPruning);
		}
		
		byte[] b = null;
		
		try
		{
			b = cc.toBytecode();
			cc.writeFile(directoryName);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

		return b;
	}
	
	public Class reloadClassFile(CtClass cc)
	{
		Class c = null;
		
		try
		{
			c = cc.toClass();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		return c;
	}
	
	public CtClass getClassFile(String className)
	{
		ClassPool pool = ClassPool.getDefault();
		CtClass cc = null;
		
		try
		{
			cc = pool.get(className);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		return cc;
	}
	
	public CtClass createClassFile(String className)
	{
		ClassPool pool = ClassPool.getDefault();
		CtClass cc = pool.makeClass(className);
		
		return cc;
	}
	
	public void permitModifyClassFile(CtClass cc)
	{
		if (!ClassPool.doPruning)
		{
			cc.defrost();
		}
	}
	
	public void insertClassPath(String directoryName)
	{
		try
		{
			ClassPool pool = ClassPool.getDefault();
			pool.insertClassPath(directoryName);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public void removeMemClassFile(CtClass cc)
	{
		cc.detach();
	}
	
	
	
	
}
